package sample.provider.dml;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.net.Uri;

/**
 * @author YUN
 */
public class SQLUpdateBuilder extends SQLBuilder {
	private SQLWhereBuilder mWhere;
	private ContentValues mValues;
	public SQLUpdateBuilder(Uri uri) {
		super(uri);
		mValues = new ContentValues();
	}
	public void addContentValue(String key, Boolean value) {
		mValues.put(key, value);
	}
	public void addContentValue(String key, Byte value) {
		mValues.put(key, value);
	}
	public void addContentValue(String key, byte[] value) {
		mValues.put(key, value);
	}
	public void addContentValue(String key, Double value) {
		mValues.put(key, value);
	}
	public void addContentValue(String key, Float value) {
		mValues.put(key, value);
	}
	public void addContentValue(String key, Integer value) {
		mValues.put(key, value);
	}
	public void addContentValue(String key, Long value) {
		mValues.put(key, value);
	}
	public void addContentValue(String key, Short value) {
		mValues.put(key, value);
	}
	public void addContentValue(String key, String value) {
		mValues.put(key, value);
	}
	public void setWhere(SQLWhereBuilder where) {
		mWhere = where;
	}
	/**
	 * <pre>
	 * ...
	 * SQLUpdateBuilder sql = new SQLUpdateBuilder(uri);
	 * sql.addContentValue("key", 0);
	 * sql.addWhere("_id", "10");
	 * int update = sql.update(getContentResolver());
	 * ...
	 * </pre>
	 * 
	 * @param contentResolver
	 * @return Number of rows updated
	 */
	public int update(ContentResolver contentResolver) {
		String where = null;
		String[] selectionArgs = null;

		// WHERE ~ AND ~
		if (mWhere != null) {
			where = mWhere.getWhere();
			selectionArgs = mWhere.getSelectionArgs();
		}

		return contentResolver.update(mUri, mValues, where, selectionArgs);
	}

}
